#include "applyfriendlist.h"
#include <QEvent>
#include <QScrollBar>
#include <QWheelEvent>

ApplyFriendList::ApplyFriendList(QWidget *parent)
    :QListWidget(parent)
{
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    this->viewport()->installEventFilter(this);
}

bool ApplyFriendList::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == this->viewport()){
        if(event->type() == QEvent::Enter){
            this->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        }else{
            this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        }
    }

    if(watched == this->viewport()){
        if(event->type() == QEvent::MouseButtonPress){
            emit sig_show_search(false);
        }
    }
    //检查事件是否是鼠标滚轮事件
    if(watched == this->viewport() && event->type() == QEvent::Wheel){
        QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
        int numDegress = wheelEvent->angleDelta().y()/8;
        int numSteps = numDegress/15;
        //设置滚动幅度
        this->verticalScrollBar()->setValue(this->verticalScrollBar()->value()-numSteps);
        return true;
    }
    return QListWidget::eventFilter(watched,event);
}
